#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;
/////////////////////////////////////
/// Basic built in types
short int si; // 16 bit
int i; // integer value (16 bit)
long int li; // 32 bit
long long int lli; // 64 bit
unsigned int ui; // unsigned integer (32 bit)
float f; // 32 bit
double d; // not less than float
char c; // 8 bit
bool b; // bolean variable (false (0) - true (not 0))
/////////////////////////////////////
/// Modosito szo
const int t = 0; // can't change the value
volatile int tt; // Volatile is a hint to the implementation to avoid aggressive optimization involving the object
// because the value of the object might be changed by means undetectable by an implementation.
extern int ttt; // You declare the existence of global variables in a header, each source file that includes
// the header knows about it, but you only need to “define” it once in one of your source files.
/////////////////////////////////////
/// References
int ii = 0;
int& r = ii;
cout << ++i << " " << ++r << "\n";
/////////////////////////////////////
/// Structs
struct Car {
int fuel;
double speed;
};
Car car;
car.speed = 100.5;
cout << car.speed << "\n";
/////////////////////////////////////
/// For loop
for(int i = 0; i < 10; ++i) {
cout << i << " ";
}
// range for loop
vector<int> vec(10, 0);
for(int& i: vec) {
i += 2;
}
// while loop
int num = 9;
while(num > -1) {
cout << vec[num] << " ";
--num;
}
/////////////////////////////////////
/// if-else
int val = 10;
if(val % 5 == 0) // modulo operator
cout << "Devided with 5\n";
else if (val == 5){
val -= 2;
cout << val << "\n";
}
else
cout << "No operation\n";
/////////////////////////////////////
/// switch case
char _c = 'a';
switch (_c) {
case 'a':
cout << "yes\n";
break;
case 'b':
cout << "No\n";
default:
break;
}
/////////////////////////////////////
/// Methods and functions
void methodWithoutReturnValue(int i, float f) {
++i;
++f;
cout << i << " " << f << "\n";
}
int iii = 1;
float fr = 2;
methodWithoutReturnValue(iii, fr);
float functionWithReturnValue(int i, float f) {
return (i + f) / 2;
}
float ff = functionWithReturnValue(iii, fr);
cout << ff;
/////////////////////////////////////
/// pointers and dinamic memory handling
// Dynamic memory is allocated using operator new and release using operator delete.
int* p = new int(10);
++(*p); // 11, dereference (get the value of the pointed memory cell)
delete p; // release the allocated memory
float* arr = new float[10];
++(*arr); // increase the first element of the array by one
float* fp = &arr[5]; // create a pointer to the 6'th element of the array
// the value of the pointer is a memory address so we get the address of
// the 6. array element using address operator (&)
++fp; // step the pointer by one to the next memory address (7. element of the array)
--(*fp); // decrease the value of the 7. element
delete[] arr; // release the array
/////////////////////////////////////
/// File handling
ifstream ifs("input.txt"); // input stream
if(!ifs.is_open())
return -1;
int tmp = 0;
string ss = "";
while (!ifs.eof()) {
ifs >> tmp >> tmp; // read two integer value from file separated by space
getline(ifs, ss); // read the remains to the end of the line in a string
}
ifs.close();
ofstream ofs("out.txt"); // output stream
for(int i = 0; i < 10; ++i)
ofs << i << " ";
ofs.close();
/////////////////////////////////////
/// String operations
string str = "abc";
int num_d = (int)'d'; // get the ascii value
int num_z = (int)'z';
for(int i = num_d; i <= num_z; ++i)
str += (char)i;
cout << str << "\n";
int idx = str.find("def");
str.replace(idx, idx+4, "DEF");
cout << str << "\n";
str[0] = 'A';
// string is an array of characters
for(int i = 0; i < str.size(); ++i)
if(str[i] == 'b' || str[i] == 'c')
str[i] = '1';
str += "0123456789"; // concatanate two string
cout << str << "\n";
/////////////////////////////////////
/// Operator overloading
class Vector {
public:
int x,y;
Vector () {};
Vector (int a,int b) : x(a), y(b) {}
// overload the "+" operator (how to add two vector)
Vector operator + (const Vector& other) {
Vector tmp;
tmp.x = this->x + other.x;
tmp.y = this->y + other.y;
return tmp;
}
};
/////////////////////////////////////
/// Class and Templates
template <class T>
class MyClass {
private:
vector<T> vec; // elements
T data;
int i;
public:
void add(T const&);
T get() const;
};
template <class T>
void MyClass<T>::add (T const& elem) {
// append copy of passed element
vec.push_back(elem);
}
template <class T>
T MyClass<T>::get () const {
if (vec.empty())
throw out_of_range("Vec is empty");
// return copy of last element
return vec.back();
}
/// function template
template <typename T>
inline T const& Max (T const& a, T const& b) {
return a < b ? b:a;
}
int t_i = 0, t_j = 1;
auto t_r = Max(t_i, t_j);
/////////////////////////////////////
/// Exceptions
class MyException: public exception {
virtual const char* what() const throw() {
return "Exception occured: negative number...";
}
};
// try to run the code
try {
int n = -1;
if(n < 0)
throw MyException();
}
// if something went wrong an exception is thrown
catch (exception& e) {
cout << e.what() << '\n';
}